package com.mou.aiagent.rag;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里云百炼RAG配置
 */
@Configuration
@Slf4j
public class LoveAppRagClouAdvisorConfig {

    @Value("${Spring.ai.dashscope.api-key}")
    private String apiKey;

    @Bean
    // 创建一个名为loveAppRagClouAdvisor的Bean
    public Advisor loveAppRagClouAdvisor() {
        // 创建一个DashScopeApi对象，传入apiKey
        DashScopeApi dashScopeApi = new DashScopeApi(apiKey);
        // 定义一个常量INDOX，值为"AI恋爱大师"
        final  String INDOX = "AI恋爱大师";
       // 创建一个DashScopeDocumentRetriever对象，传入dashScopeApi和DashScopeDocumentRetrieverOptions对象
       DocumentRetriever dashScopeDocumentRetriever = new DashScopeDocumentRetriever(dashScopeApi,
                DashScopeDocumentRetrieverOptions.builder()
                        .withIndexName(INDOX)
                        .build());
       // 创建一个RetrievalAugmentationAdvisor对象，传入dashScopeDocumentRetriever
       return RetrievalAugmentationAdvisor.builder()
               .documentRetriever(dashScopeDocumentRetriever)
               .build();
    }
}
